'
' File.vb
' uAPI Public License
' @author Bastie
'
Namespace uapi.io
    Public Class File
        Public path As String = ""

        Public Sub New()
            Me.path = "\"
        End Sub

        Public Sub New(ByRef newPath As String)
            Me.path = newPath
        End Sub


        Public Function exist() As Boolean
            If isFile() Then
                Return My.Computer.FileSystem.FileExists(Me.path)
            Else
                Return My.Computer.FileSystem.DirectoryExists(Me.path)
            End If
        End Function

        Public Function isDirectory() As Boolean
            Dim lastChar As String
            lastChar = Microsoft.VisualBasic.Right(Me.path, 1)
            Return lastChar = "\"
        End Function

        Public Function isFile() As Boolean
            Dim lastChar As String
            lastChar = Microsoft.VisualBasic.Right(Me.path, 1)
            Return lastChar <> "\"
        End Function


        Public Function makeDirectory() As Boolean
            Try
                My.Computer.FileSystem.CreateDirectory(Me.path)
            Catch ex As Exception
                Return False
            End Try
            Return True
        End Function
        Public Function del() As Boolean
            Try
                If Me.isFile Then
                    Kill(Me.path)
                Else
                    RmDir(Me.path)
                End If
            Catch ex As Exception
                Return False
            End Try
            Return True
        End Function

        Public Function rename(ByRef newName As String) As Boolean
            Dim ok As Boolean = True
            Try
                If isFile() Then
                    My.Computer.FileSystem.RenameFile(Me.path, newName)
                Else
                    My.Computer.FileSystem.RenameDirectory(Me.path, newName)
                End If
            Catch ex As Exception
                ok = False
            End Try
            If (ok) Then
                Me.path = newName
            End If
            Return ok
        End Function

        Public Overrides Function toString() As String
            Return Me.path
        End Function

        Public Function listFiles() As File()
            Dim fileArr() As File = {} ' NullPattern
            Dim dirsAndFiles As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
            If Me.isDirectory And Me.exist Then
                dirsAndFiles = My.Computer.FileSystem.GetDirectories(Me.path)
                For Each dirName As String In dirsAndFiles
                    ReDim fileArr(UBound(fileArr) + 1)
                    fileArr(UBound(fileArr)) = New File(dirName)
                Next
                dirsAndFiles = My.Computer.FileSystem.GetFiles(Me.path)
                For Each fileName As String In dirsAndFiles
                    ReDim fileArr(UBound(fileArr) + 1)
                    fileArr(UBound(fileArr)) = New File(fileName)
                Next
            End If
            Return fileArr
        End Function


        Protected Overrides Sub Finalize()
            MyBase.Finalize()
        End Sub
    End Class
End Namespace
